package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	. "xm_shop/database"
	. "xm_shop/models"
)

type CartController struct{}

// 获取购物车数据
func (c CartController) Get(context *gin.Context) {
	var cartList []Cart
	Cookie.Get(context, "cartList", &cartList)

	var allPrice float64

	// 总价格
	for i := 0; i < len(cartList); i++ {
		if cartList[i].Checked { // 选中才统计
			allPrice += cartList[i].Price * float64(cartList[i].Num)
		}
	}

	context.JSON(http.StatusOK, gin.H{
		"success":  true,
		"cartList": cartList,
		"allPrice": allPrice,
	})
}

// 添加商品到购物车
func (c CartController) AddCart(context *gin.Context) {
	// 1. 获取增加购物车的数据
	colorId, err1 := strconv.Atoi(context.Query("color_id"))
	goodsId, err2 := strconv.Atoi(context.Query("goods_id"))
	if err1 != nil || err2 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 检索商品
	var goods Goods
	var goodsColor GoodsColor
	DB.Where("id = ?", goodsId).Take(&goods)
	DB.Where("id = ?", colorId).Take(&goodsColor)
	// 2.创建购物车对象  // 这个不是保存在数据库
	currentData := Cart{
		ID:           goodsId,
		Title:        goods.Title,
		Price:        goods.Price,
		GoodsVersion: goods.GoodsVersion,
		Num:          1,
		GoodsColor:   goodsColor.ColorName,
		GoodsImg:     goods.GoodsImg,
		GoodsGift:    goods.GoodsGift,
		GoodsAttr:    "",
		Checked:      true,
	}
	// 3. 判断购物车有没有数据
	var cartList []Cart
	Cookie.Get(context, "cartList", &cartList)
	if len(cartList) > 0 {
		// 购物车有数据, 判断是否存在当前商品
		if HasCartData(cartList, currentData) { // 表示购物车已经存在相同商品, 数量 + 1
			for i := 0; i < len(cartList); i++ {
				if cartList[i].ID == currentData.ID && cartList[i].GoodsColor == currentData.GoodsColor && cartList[i].GoodsAttr == currentData.GoodsAttr {
					cartList[i].Num += 1
				}
			}
		} else {
			cartList = append(cartList, currentData)
		}
		// 保存到cookie
		Cookie.Set(context, "cartList", cartList)
	} else {
		// 如果没有购物车列表, 或者购物车列表长度为0, 新建购物车
		cartList = append(cartList, currentData)
		Cookie.Set(context, "cartList", cartList)
	}
	context.String(http.StatusOK, "购物车添加成功")
}

// 增加已经存在的购物车商品数量
func (c CartController) IncCart(context *gin.Context) {
	goodsId, err1 := strconv.Atoi(context.Query("goods_id"))
	goodsColor := context.Query("goods_color")
	goodsAttr := ""
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 定义返回的数据
	var allPrice float64
	var currentPrice float64
	var num int
	// 在cookie中获取购物车
	var cartList []Cart
	Cookie.Get(context, "cartList", &cartList)
	if len(cartList) <= 0 {
		context.String(http.StatusBadRequest, "购物车数据异常")
		return
	}
	for i := 0; i < len(cartList); i++ {
		if cartList[i].ID == goodsId && cartList[i].GoodsColor == goodsColor && cartList[i].GoodsAttr == goodsAttr {
			cartList[i].Num += 1
			currentPrice = cartList[i].Price * float64(cartList[i].Num)
			num = cartList[i].Num
		}
		if cartList[i].Checked { // 统计总价格
			allPrice += cartList[i].Price * float64(cartList[i].Num)
		}
	}
	// 写入cookie
	Cookie.Set(context, "cartList", cartList)

	context.JSON(http.StatusOK, gin.H{
		"success":      true,
		"message":      "更新成功",
		"allPrice":     allPrice,
		"currentPrice": currentPrice,
		"num":          num,
	})
}

// 减少已经存在的购物车商品数量
func (c CartController) DecCart(context *gin.Context) {
	goodsId, err1 := strconv.Atoi(context.Query("goods_id"))
	goodsColor := context.Query("goods_color")
	goodsAttr := ""
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 定义返回的数据
	var allPrice float64
	var currentPrice float64
	var num int
	// 在cookie中获取购物车
	var cartList []Cart
	Cookie.Get(context, "cartList", &cartList)
	if len(cartList) <= 0 {
		context.String(http.StatusBadRequest, "购物车数据异常")
		return
	}
	for i := 0; i < len(cartList); i++ {
		if cartList[i].ID == goodsId && cartList[i].GoodsColor == goodsColor && cartList[i].GoodsAttr == goodsAttr {
			if cartList[i].Num > 1 {
				cartList[i].Num -= 1
			}
			currentPrice = cartList[i].Price * float64(cartList[i].Num)
			num = cartList[i].Num
		}
		if cartList[i].Checked { // 统计总价格
			allPrice += cartList[i].Price * float64(cartList[i].Num)
		}
	}
	// 写入cookie
	Cookie.Set(context, "cartList", cartList)

	context.JSON(http.StatusOK, gin.H{
		"success":      true,
		"message":      "更新成功",
		"allPrice":     allPrice,
		"currentPrice": currentPrice,
		"num":          num,
	})
}

// 修改单个购物车商品状态
func (c CartController) ChangeOneCart(context *gin.Context) {
	// 获取参数
	goodsId, err1 := strconv.Atoi(context.Query("goods_id"))
	goodsColor := context.Query("goods_color")
	goodsAttr := ""
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数不合法")
		return
	}
	var cartList []Cart
	Cookie.Get(context, "cartList", &cartList)
	if len(cartList) == 0 {
		context.String(http.StatusBadRequest, "购物车数据异常")
		return
	}
	var allPrice float64 // 总价格
	for i := 0; i < len(cartList); i++ {
		if cartList[i].ID == goodsId && cartList[i].GoodsColor == goodsColor && cartList[i].GoodsAttr == goodsAttr {
			cartList[i].Checked = !cartList[i].Checked
		}
		if cartList[i].Checked {
			allPrice += cartList[i].Price * float64(cartList[i].Num)
		}
	}
	// 重新写入cookie
	Cookie.Set(context, "cartList", cartList)
	context.JSON(http.StatusOK, gin.H{
		"success":  true,
		"allPrice": allPrice,
	})
}

// 修改全部购物车商品的状态
func (c CartController) ChangeAllCart(context *gin.Context) {
	flag, err1 := strconv.Atoi(context.Query("flag"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	var cartList []Cart
	// 取cookie
	Cookie.Get(context, "cartList", &cartList)
	if len(cartList) <= 0 {
		context.String(http.StatusBadRequest, "购物车数据异常")
		return
	}
	var allPrice float64 // 总价格
	for i := 0; i < len(cartList); i++ {
		if flag == 1 {
			cartList[i].Checked = true
		} else {
			cartList[i].Checked = false
		}
		if cartList[i].Checked {
			allPrice += cartList[i].Price * float64(cartList[i].Num)
		}
	}
	// 写入cookie
	Cookie.Set(context, "cartList", cartList)
	context.JSON(http.StatusOK, gin.H{
		"success":  true,
		"allPrice": allPrice,
	})
}

// 删除购物车商品
func (c CartController) DelCart(context *gin.Context) {
	// 获取参数
	goodsId, err1 := strconv.Atoi(context.Query("goods_id"))
	goodsColor := context.Query("goods_color")
	goodsAttr := ""
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数不合法")
		return
	}
	var cartList []Cart
	// 取cookie
	Cookie.Get(context, "cartList", &cartList)
	for i := 0; i < len(cartList); i++ {
		if cartList[i].ID == goodsId && cartList[i].GoodsColor == goodsColor && cartList[i].GoodsAttr == goodsAttr {
			cartList = append(cartList[:i], cartList[(i+1):]...)
		}
	}
	// 写入cookie
	Cookie.Set(context, "cartList", cartList)
	context.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "删除成功",
	})
}
